import * as types from '../mutation-types';

const state ={
    buttonLoading:false,
    searchLoading:false,
    activityId  :'',
    activityName:'',
    creator     :'',
    department  :[{
        value: 'beijing',
        label: '北京',
        children: [],
        loading: false
    },{
        value: 'hangzhou',
        label: '杭州',
        children: [],
        loading:false
    }],
    child       :[]
}

const getters = {
    activityId  :  state => state.activityId,
    activityName:  state => state.activityName,
    creator     :  state => state.creator,
    department  :  state => state.department,
    child       :  state => state.child,
    buttonLoading: state => state.buttonLoading
}

const actions = {
    changeActivityId({commit},e){
       commit(types.UPDATE_ACTIVITYID,e.target.value)
    },
    searchActivityList(store){
       store.commit(types.BUTTON_LOADING,true);
       store.commit(types.SEARCH_LOADING,true);
       setTimeout(()=>{
           store.commit(types.BUTTON_LOADING,false);
           store.commit(types.SEARCH_ACTIVITY_LIST)
       },1000)
    },
    fethChildDepartment(store,obj){
        let childItem = store.state.department.filter(function(item){
            return item.value == obj.value;
        })
        obj['item'] = childItem[0]
        obj['bool'] = true
        store.commit(types.CASCADER_LOADING,obj);
        setTimeout(()=>{
            let child = [];
            if (obj.item.value === 'beijing') {
                child = [
                    {
                        value: 'talkingdata',
                        label: 'TalkingData'
                    },
                    {
                        value: 'baidu',
                        label: '百度'
                    },
                    {
                        value: 'sina',
                        label: '新浪'
                    }
                ];
            } else if (obj.item.value === 'hangzhou') {
                child = [
                    {
                        value: 'ali',
                        label: '阿里巴巴'
                    },
                    {
                        value: '163',
                        label: '网易'
                    }
                ];
            }
            obj['child'] = child;
            store.commit(types.SEARCH_CHILD_DEPARTMENT,obj);
            obj['bool'] = false;
            store.commit(types.CASCADER_LOADING,obj);
        },1000)
    }

}

const mutations = {
    [types.UPDATE_ACTIVITYID] (state, val) {
        state.activityId = val;
    },
    [types.BUTTON_LOADING](state,bool){
        state.buttonLoading = bool;
    },
    [types.SEARCH_LOADING](state,bool){
        state.searchLoading = bool
    },
    [types.SEARCH_ACTIVITY_LIST](state){

    },
    [types.CASCADER_LOADING](state,obj){
        obj.item.loading = obj.bool
    },
    [types.SEARCH_CHILD_DEPARTMENT](state,obj){
        obj.item.children = obj.child
        obj.callback();
    }

}

export default {
    state,
    getters,
    actions,
    mutations
}